/**
 * Created with JetBrains PhpStorm.
 * User: ishowshao
 * Date: 12-12-11
 * Time: 下午8:02
 * To change this template use File | Settings | File Templates.
 */
var ua = window.navigator.userAgent;
var isMobile = /mobile/i.test(ua);
var editor = jQuery('#editor');
editor.show();
var App = {};
App.posts = jQuery('#posts');

/**
 * 添加回复按钮的事件
 */
App.posts.show().delegate('.open-comment', 'click', function (e) {
    e.preventDefault();
    jQuery(this).hide().siblings('.comment-container').show();
});

/**
 * 提交回复
 */
App.posts.delegate('.submit-comment', 'click', function (e) {
    e.preventDefault();
    var commentField = jQuery(this).siblings('input[name=comment]');
    var comment = commentField.val();
    var id = commentField.data('id');
    console.log(comment);
    console.log(commentField.data('id'));
    if (comment !== '' && id) {
        jQuery.post('comment.php', { time: id, comment: comment }, function (result) {
            if (result.success) {
                commentField.val('');
            }
        }, 'json');
    }
});

jQuery.get('read.php', function (result) {
    jQuery.each(result, function (index, data) {
        jQuery('#posts').append(createPost(data));
    });
    jQuery('#load-more-wrap').show();
}, 'json');

jQuery('#take-photo').click(function (e) {
    e.preventDefault();
    jQuery('#take-photo-input').click();
});

var createPost = function (data) {
    var post = jQuery('<div class="post"></div>');
    var postHeader = jQuery('<div class="post-header"></div>');

    var action = data['creator'];
    if (data['twitter'].length > 0) {
        action += ' 记录了心情';
    }
    if (data['photos'].length > 0) {
        action += ' 上传了' + data['photos'].length + '张照片';
    }

    postHeader.append('<div class="post-header-action">' + action + '</div>');
    postHeader.append('<div class="post-header-time">' + formatDate(data['time']) + '</div>');
    post.append(postHeader);

    if (data.twitter) {
        post.append('<div class="post-content">' + data.twitter + '</div>');
    }

    if (data['photos'].length > 0) {
        var postImages = jQuery('<div class="post-images"></div>');
        var postImagesWrap = jQuery('<div class="post-images-wrap"></div>');
        jQuery.each(data['photos'], function (i, item) {
            if (isMobile) {
                postImagesWrap.append('<img src="data/thumbs/' + item + '">');
            } else {
                postImagesWrap.append('<img src="data/photos/' + item + '">');
            }
        });
        postImages.append(postImagesWrap);
        post.append(postImages);
    }
    //如果有评论存在
    if (typeof data['comments'] !== 'undefined' && data['comments'].length > 0) {
        var commentsContainer = jQuery('<div class="post-comments"></div>');
        jQuery.each(data['comments'], function (i, comment) {
            commentsContainer.append('<div>' + comment['user'] + ': ' + comment['content'] + '</div>');
        });
        post.append(commentsContainer);
    }
    post.append('<div class="post-operation"><div class="comment-container"><input type="text" name="comment" data-id="' + data['time'] + '" /><a href="" class="submit-comment">提交</a></div><a href="" class="open-comment">回复</a></div>');
    return post;
};

var formatDate = function (time) {
    var date = new Date();
    date.setTime(time * 1000);
    var hours = date.getHours();
    if (String(hours).length == 1) {
        hours = '0' + String(hours);
    }
    var minutes = date.getMinutes();
    if (String(minutes).length == 1) {
        minutes = '0' + String(minutes);
    }
    return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日 ' + hours + ':' + minutes;
};

var onUploaded = function (data) {
    if (data) {
        var post = createPost(data);
        jQuery('#posts').prepend(post);

        jQuery('#twitter').val('');
        jQuery('#take-photo-input').val('');
    }
    jQuery('#editor').find('input[type=submit]').val('提交').removeAttr('disabled');
};

//用一个position fixed的底部像素，和body底部的像素的offset top做差值 是一个有效的检测内容滚动到底部的方案
var currentPage = 1;
var isEof = false;
jQuery('#load-more').click(function () {
    if (!isEof) {
        var button = jQuery(this);
        button.attr('disabled', 'disabled').html('加载中');
        jQuery.get('read.php?page=' + String(currentPage + 1), function (result) {
            button.removeAttr('disabled').html('加载更多');
            jQuery.each(result, function (index, data) {
                jQuery('#posts').append(createPost(data));
            });
            if (result.length > 0) {
                currentPage++;
            } else {
                isEof = true;
            }
        }, 'json');
    }
});

editor.on('submit', function () {
    jQuery(this).find('input[type=submit]').val('提交中').attr('disabled', 'disabled');
});

var faceString = '[微笑][撇嘴][色][发呆][得意][流泪][害羞][闭嘴][睡][大哭][尴尬][发怒][调皮][呲牙][惊讶][难过][酷][冷汗][抓狂][吐][偷笑][愉快][白眼][傲慢][饥饿][困][惊恐][流汗][憨笑][悠闲][奋斗][咒骂][疑问][嘘][晕][疯了][衰][骷髅][敲打][再见][擦汗][抠鼻][鼓掌][糗大了][坏笑][左哼哼][右哼哼][哈欠][鄙视][委屈][快哭了][阴险][亲亲][吓][可怜][菜刀][西瓜][啤酒][篮球][乒乓][咖啡][饭][猪头][玫瑰][凋谢][嘴唇][爱心][心碎][蛋糕][闪电][炸弹][刀][足球][瓢虫][便便][月亮][太阳][礼物][拥抱][强][弱][握手][胜利][抱拳][勾引][拳头][差劲][爱你][NO][OK][爱情][飞吻][跳跳][发抖][怄火][转圈][磕头][回头][跳绳][投降][激动][乱舞][献吻][左太极][右太极]';
